#include <cstdlib>
#include <iostream>
#include <sstream>
#include <windows.h>
#include <fstream>

using namespace std;

void SaveAcc();
void Encrypt();
void Decrypt();
string char2String(char temp[]);
void wait(int second);
int str2Int(string temp);
void OpenAcc();

//Global
int AccNum,Pass;  //---------|
int Balance;               //|
string Acc;                //| Var
int AccNum_Enc,Pass_Enc;   //|
int Balance_Enc;           //|
//End


int main(int argc, char *argv[])
{
    int account,deposit,time;
    Acc = char2String(argv[1]);
    account = str2Int(char2String(argv[1]));
    Pass = str2Int(char2String(argv[2]));
    deposit = str2Int(char2String(argv[3]));
    time = str2Int(char2String(argv[4]))*60;
    wait(time);
    OpenAcc();
    Decrypt();
    Balance += deposit;
    Encrypt();
    SaveAcc();
    return EXIT_SUCCESS;
}

int str2Int(string temp)
{
       return atoi(temp.c_str());
}

void wait(int second)
{
     for(int i(0); i<=second; i++)
     {
        Sleep(999);
     }
}

string char2String(char temp[])
{
       string str;
       for(int i(0); i<sizeof(temp)+1; i++)
       str += temp[i];
       return str;
}

void Decrypt()
{
     string temp;
     string temp2;
             AccNum = AccNum_Enc ^ Pass;
             Pass = Pass_Enc ^ 18;
             Balance = Balance_Enc ^ Pass;
             /*temp = XOR(AccNum_Enc,Pwd);
             AccNum = atoi(base64_decode(temp).c_str());

             temp = XOR(Pass_Enc,Pwd);
             Pass = atoi(base64_decode(temp).c_str());

             temp = XOR(Balance_Enc,Pwd);
             Balance = atoi(base64_decode(temp).c_str());*/
}

void Encrypt()
{
     string temp;
     string temp2;
     //Pwd = int2Str(Pass);
             AccNum_Enc = AccNum ^ Pass;
             Pass_Enc = Pass ^ 18;
             Balance_Enc = Balance ^ Pass;
             /*
             temp = base64_encode(reinterpret_cast<const unsigned char*>(int2Str(AccNum).c_str()), int2Str(AccNum).length());
             AccNum_Enc = XOR(temp,Pwd);

             temp = base64_encode(reinterpret_cast<const unsigned char*>(int2Str(Pass).c_str()), int2Str(Pass).length());
             Pass_Enc = XOR(temp,Pwd);

             temp = base64_encode(reinterpret_cast<const unsigned char*>(int2Str(Balance).c_str()), int2Str(Balance).length());
             Balance_Enc = XOR(temp,Pwd);*/
}

void SaveAcc()
{
     Encrypt();
     ofstream Save;
     //cout << "Save:" << (Acc + ".mb").c_str()<< endl;
     Save.open((Acc + ".mb").c_str());
     Save << Balance_Enc << endl;
     Save << AccNum_Enc << endl;
     Save << Pass_Enc << endl;
     Save.close();
}

void OpenAcc()
{
               ifstream Read((Acc + ".mb").c_str(),ios::in);
               Read >> Balance_Enc;
               Read >> AccNum_Enc;
               Read >> Pass_Enc;
}
